import React, {Component} from 'react';
import {inject, observer} from 'mobx-react';
import {Spin} from 'antd';
import {WeaPopoverHrm, WeaNewScrollPagination, WeaShare} from 'ecCom';
import SearchFavourite from '../../util/SearchFavourite';

import SysFavouriteBar from './SysFavouriteBar';
import SysFavouriteList from './SysFavouriteList';
import EditSysFavourite from './EditSysFavourite';
import FavouriteBrowser from './FavouriteBrowser';

@inject('sysFavouriteStore', 'myFavouriteStore')
@observer
export default class SysFavourite extends Component {
    constructor(props) {
        super(props);
        this.state = {favListHeight: 0};
    }

    /**
     * 滚动加载收藏夹的内容
     */
    handleScroll = () => {
        const {sysFavouriteStore} = this.props;
        const offset = jQuery('.sysfav-item-loadding').position();
        const winHeight = jQuery(document.body).height();
        const {top} = offset;

        const conditions = SearchFavourite.getConditions();
        const {iscomplete} = conditions;

        if (top <= winHeight && iscomplete) { // 说明最下方的div可见，加载下一页的数据
            const searchFavourite = new SearchFavourite(sysFavouriteStore);
            const isinit = false;
            const isreload = false;
            const params = {isinit, isreload};
            searchFavourite.loadPage(params);
        }
    }

    fetchData = () => {
        const {sysFavouriteStore} = this.props;
        const conditions = SearchFavourite.getConditions();
        const {iscomplete} = conditions;
        if (iscomplete) { // 加载下一页的数据
            const searchFavourite = new SearchFavourite(sysFavouriteStore);
            const isinit = false;
            const isreload = false;
            const params = {isinit, isreload};
            searchFavourite.loadPage(params);
        }
    }


    /**
     * 设置加载的提示信息
     */
    setLoaddingMsg = () => {
        const {myFavouriteStore, sysFavouriteStore} = this.props;
        const {loadingmsg, nodata, nomoredata} = myFavouriteStore;
        const {spinning} = sysFavouriteStore;
        const {total, current, iscomplete} = SearchFavourite.getConditions();

        if (!spinning) {
            if (iscomplete) {
                if (current == total) {
                    jQuery('.sysfav-item-loadding').html(nomoredata);
                }
                if (total <= 0) {
                    jQuery('.sysfav-item-loadding').html(nodata);
                }
                if (current < total) {
                    jQuery('.sysfav-item-loadding').html(loadingmsg);
                }
            } else {
                jQuery('.sysfav-item-loadding').html(loadingmsg);
            }
        }
    }

    onVisibleChange = bool => {
        const {sysFavouriteStore} = this.props;
        sysFavouriteStore.shareShow = bool;
    }

    componentDidUpdate() {
        this.setLoaddingMsg();
        if (this.props.changename == 'fullPagetreeRight') {
            jQuery('.fullPagetreeRight .ant-spin-container').css({height: '100%'});
        } else {
            jQuery('.sysfav-main .ant-spin-container').css({height: '100%'});
        }

        this.resetHeight();
    }

    componentDidMount() {
        this.setLoaddingMsg();
        this.resetHeight();
    }

    resetHeight = () => {
        const oheight = this.state.favListHeight;
        let favListHeight = jQuery('.sysfav-main').height();
        if (this.props.changename == 'fullPagetreeRight') {
            favListHeight = jQuery('.fullPagetreeRight').height();
        }

        if (favListHeight > 51) {
            favListHeight = favListHeight - 51;
        }
        if (oheight != favListHeight) {
            this.setState({favListHeight});
        }
    }

    render() {
        const {sysFavouriteStore} = this.props;

        const {spinning, tip} = sysFavouriteStore;

        const {favListHeight} = this.state;
        return (
            <div className={this.props.changename == 'fullPagetreeRight' ? 'fullPagetreeRight' : 'sysfav-main'}>
                <Spin
                    ecId={`${this && this.props && this.props.ecId || ''}_Spin@rjcpq1`}
                    spinning={spinning}
                    tip={tip}
                    size="large"
                    style={{height: '100%'}}
                >
                    <SysFavouriteBar
                        ecId={`${this && this.props && this.props.ecId || ''}_SysFavouriteBar@iy7r6w`}
                    />

                    <div className="sysfav-main-list">
                        <WeaPopoverHrm ecId={`${this && this.props && this.props.ecId || ''}_WeaPopoverHrm@d4t0sb`}>
                            <WeaNewScrollPagination
                                ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScrollPagination@d7vo3n`}
                                onScrollEnd={this.fetchData}
                                height={favListHeight}
                            >
                                <SysFavouriteList
                                    ecId={`${this && this.props && this.props.ecId || ''}_SysFavouriteList@ny7kft`}
                                    setLoaddingMsg={this.setLoaddingMsg}
                                />
                                <div className="sysfav-item-loadding"/>
                            </WeaNewScrollPagination>
                        </WeaPopoverHrm>

                        <EditSysFavourite
                            ecId={`${this && this.props && this.props.ecId || ''}_EditSysFavourite@eitbmx`}
                        />

                        <FavouriteBrowser
                            ecId={`${this && this.props && this.props.ecId || ''}_FavouriteBrowser@0r3aga`}
                        />

                        <WeaShare
                            ecId={`${this && this.props && this.props.ecId || ''}_WeaShare@4szr5x`}
                            visible={sysFavouriteStore.shareShow}
                            onVisibleChange={this.onVisibleChange}
                            shareParams={{
                                resourcetype: 152,
                                resourceid: sysFavouriteStore.sharedata.favid,
                            }}
                            msginfo={[
                                {
                                    sharetitle: sysFavouriteStore.sharedata.favname,
                                    linkurl: sysFavouriteStore.sharedata.url,
                                    objectName: 'FW:CustomShareMsg',
                                    canforward: 1,
                                    sharetype: 'workflow',
                                    shareid: sysFavouriteStore.sharedata.favid,
                                    opentype: 1,
                                    opentype_pc: 2,
                                },
                            ]}
                        />
                    </div>
                </Spin>
            </div>
        );
    }
}
